package com.studyyoun.android.nonghe.home.item;

/**
 * Created by androidlongs on 17/5/1.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */


import android.content.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.text.TextUtils;
import android.view.*;
import android.widget.*;


import com.studyyoun.android.androidcommonbaselibrary.glide.CommonGlideUtils;
import com.studyyoun.android.androidcommonbaselibrary.utils.BitmapUtils;
import com.studyyoun.android.androidcommonbaselibrary.utils.LogUtils;
import com.studyyoun.android.nonghe.R;
import com.studyyoun.android.nonghe.http.HttpUrlHelper;
import com.studyyoun.android.nonghe.person.activity.PersonActivity;
import com.studyyoun.android.nonghe.welcom.App;
import com.studyyoun.android.nonghe.welcom.CommonUserHelper;
import com.studyyoun.android.nonghe.welcom.activity.LoginActivity;
import com.studyyoun.android.nonghe.welcom.model.UserModel;

import java.lang.ref.*;

import de.hdodenhof.circleimageview.CircleImageView;

import static android.content.Intent.*;

/**
 * 个人页面  头部功能封装
 */

public class ItemHeader {

    private final LinearLayout mHeaderLoginLineLayout;
    private final LinearLayout mHeaderInfoLineLayout;
    private final TextView mUserNameTextView;
    private final TextView mUserFlagTextView;
    private final CircleImageView mHeaderImageView;

    public ItemHeader(LinearLayout headerBgRelatLayout) {
        mHeaderLoginLineLayout = (LinearLayout) headerBgRelatLayout.findViewById(R.id.ll_person_header_login);
        Button headerLoginButton = (Button) headerBgRelatLayout.findViewById(R.id.bt_person_header_login);

        mHeaderInfoLineLayout = (LinearLayout) headerBgRelatLayout.findViewById(R.id.ll_person_header_info);

        mUserNameTextView = (TextView) mHeaderInfoLineLayout.findViewById(R.id.ll_person_header_info_name);
        mUserFlagTextView = (TextView) mHeaderInfoLineLayout.findViewById(R.id.ll_person_header_info_flag);

        mHeaderImageView = mHeaderInfoLineLayout.findViewById(R.id.iv_person_header_img);

        headerLoginButton.setOnClickListener(mLoginOnClickListener);
        mUserNameTextView.setOnClickListener(mUserNmaeOnClickListener);
        mUserFlagTextView.setOnClickListener(mUserFlagOnClickListener);

        setHeaderBg(headerBgRelatLayout);
    }


    /**
     * 设置头部页面的 背景
     */
    private void setHeaderBg(LinearLayout headerBgRelatLayout) {
        SoftReference<Bitmap> softReference = BitmapUtils.getInstance().getScaleBitmap(App.getInstance().getResources(), R.mipmap.person_header_bg);
        if (softReference != null && softReference.get() != null) {
            headerBgRelatLayout.setBackgroundDrawable(new BitmapDrawable(softReference.get()));
        }
    }

    /**
     * 登录按钮交互
     */
    private View.OnClickListener mLoginOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            App.getInstance().startActivity(new Intent(App.getInstance(), LoginActivity.class).addFlags(FLAG_ACTIVITY_NEW_TASK));
        }
    };
    /**
     * 用户姓名点击交换
     */
    private View.OnClickListener mUserNmaeOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            App.getInstance().startActivity(new Intent(App.getInstance(), PersonActivity.class).addFlags(FLAG_ACTIVITY_NEW_TASK));
        }
    };
    /**
     * 用户昵称 交换
     */
    private View.OnClickListener mUserFlagOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            App.getInstance().startActivity(new Intent(App.getInstance(), PersonActivity.class).addFlags(FLAG_ACTIVITY_NEW_TASK));
        }
    };


    /**
     * 显示 登录接口 隐藏用户信息显示
     */
    public void showLoginPage() {
        mHeaderLoginLineLayout.setVisibility(View.VISIBLE);
        mHeaderInfoLineLayout.setVisibility(View.GONE);
    }

    /**
     * 显示 用户信息  隐藏 登录接口
     */
    public void dimissLoginPage() {
        mHeaderLoginLineLayout.setVisibility(View.GONE);
        mHeaderInfoLineLayout.setVisibility(View.VISIBLE);
        //设置用户的信息的显示
        setUserInfoShow();
    }

    /**
     * 设置用户的信息的显示
     */
    private void setUserInfoShow() {
        UserModel user = CommonUserHelper.getUserModel();
        if (user != null) {
            mUserFlagTextView.setText(user.getUserFlag() + "");
            mUserNameTextView.setText(user.getNixe());
            String imgUrl = user.getImgUrl();
            if (TextUtils.isEmpty(imgUrl)) {
                mHeaderImageView.setImageDrawable(mHeaderImageView.getResources().getDrawable(R.mipmap.default_title));
            }else {
                imgUrl = HttpUrlHelper.BaseImageHost+imgUrl;
                LogUtils.d("加载头像 "+imgUrl);
                CommonGlideUtils.showImageView(mHeaderImageView.getContext(), R.mipmap.default_title, imgUrl, mHeaderImageView);
            }

        } else {
            showLoginPage();
        }
    }
}
